﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace PhilosophyQuiz
{
	public class Question
	{
		public enum Subject
		{
			BEL,
			Mathemathics,
			History,
			Physics,
			Chemistry,
			Literacy,
			Biology,
			Geography,
			Philosophy
		}
		
		public int ClassNumber { get; set; }
		public string Category { get; set; }
		public int QuestionsCount { get; set; }
		public string QuestionT { get; set; }
		public List<Tuple<string, bool>> Answers { get; set; }
		public bool Taken { get; set; }
		public string path = Directory.GetCurrentDirectory() + "/";
				
		public List<Question> GetXmlData(Subject subject)
		{
			switch (subject)
			{
				case Subject.BEL:
					path = Directory.GetCurrentDirectory() + "/data/bel.xml";
					break;
				case Subject.Mathemathics:
					path = Directory.GetCurrentDirectory() + "/data/mathematics.xml";
					break;
				case Subject.History:
					path = Directory.GetCurrentDirectory() + "/data/history.xml";
					break;
				case Subject.Physics:
					path = Directory.GetCurrentDirectory() + "/data/physics.xml";
					break;
				case Subject.Chemistry:
					path = Directory.GetCurrentDirectory() + "/data/chemistry.xml";
					break;
				case Subject.Literacy:
					path = Directory.GetCurrentDirectory() + "/data/literacy.xml";
					break;
				case Subject.Biology:
					path = Directory.GetCurrentDirectory() + "/data/biology.xml";
					break;
				case Subject.Geography:
					path = Directory.GetCurrentDirectory() + "/data/geography.xml";
					break;
				case Subject.Philosophy:
					path = Directory.GetCurrentDirectory() + "/data/philosophy.xml";
					break;
			}

			// Зареждаме документа
			XmlDocument data = new XmlDocument();
			List<Question> listQuestion = new List<Question>();
			data.Load(path);

			// Цикъл за извличане на елементите от файла и попълване на колекцията
			for (int i = 1; i < data.DocumentElement.ChildNodes.Count; i++)
			{
				// Проверяваме дали избраният клас за игра съвпада с въпросите за съответния клас
				if (data.DocumentElement.ChildNodes[i].Attributes[0].InnerText == ClassNumber.ToString())
				{
					foreach (XmlNode question in data.DocumentElement.ChildNodes[i].ChildNodes)
					{
						var ListTupleAnswers = new List<Tuple<string, bool>>();
						for (int j = 0; j < 4; j++)
						{
							var tupleQuestion = new Tuple<string, bool>(question.ChildNodes[j].Attributes[0].InnerText,
							Boolean.Parse(question.ChildNodes[j].Attributes[1].InnerText));
							ListTupleAnswers.Add(tupleQuestion);
						}
						listQuestion.Add(new Question()
						{
							Category = data.DocumentElement.ChildNodes[i].Attributes[0].InnerText,
							QuestionT = question.Attributes[0].InnerText,
							Answers = ListTupleAnswers,
						});
					}
				}
			}
			QuestionsCount = listQuestion.Count;
			return listQuestion;
		}
	}
}
